package com.kede.app.service.impl;

import com.kede.app.domain.UnishopProduct;
import com.kede.app.repository.UnishopProductRepository;
import com.kede.app.service.UnishopProductService;
import com.kede.app.service.dto.UnishopProductDto;
import com.kede.app.service.dto.UnishopProductQueryCriteria;
import com.kede.app.service.mapstruct.UnishopProductMapper;
import com.kede.utils.FileUtil;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import com.kede.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-06-17
**/
@Service
@RequiredArgsConstructor
public class UnishopProductServiceImpl implements UnishopProductService {

    private final UnishopProductRepository unishopProductRepository;
    private final UnishopProductMapper unishopProductMapper;

    @Override
    public Map<String,Object> queryAll(UnishopProductQueryCriteria criteria, Pageable pageable){
        Page<UnishopProduct> page = unishopProductRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(unishopProductMapper::toDto));
    }

    @Override
    public List<UnishopProductDto> queryAll(UnishopProductQueryCriteria criteria){
        return unishopProductMapper.toDto(unishopProductRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public UnishopProductDto findById(Integer id) {
        UnishopProduct unishopProduct = unishopProductRepository.findById(id).orElseGet(UnishopProduct::new);
        ValidationUtil.isNull(unishopProduct.getId(),"UnishopProduct","id",id);
        return unishopProductMapper.toDto(unishopProduct);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UnishopProductDto create(UnishopProduct resources) {
        return unishopProductMapper.toDto(unishopProductRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UnishopProduct resources) {
        UnishopProduct unishopProduct = unishopProductRepository.findById(resources.getId()).orElseGet(UnishopProduct::new);
        ValidationUtil.isNull( unishopProduct.getId(),"UnishopProduct","id",resources.getId());
        unishopProduct.copy(resources);
        unishopProductRepository.save(unishopProduct);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            unishopProductRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<UnishopProductDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (UnishopProductDto unishopProduct : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("分类ID", unishopProduct.getCategoryId());
            map.put("产品标题", unishopProduct.getTitle());
            map.put("主图", unishopProduct.getImage());
            map.put("组图", unishopProduct.getImages());
            map.put("详情", unishopProduct.getDesc());
            map.put("市场价", unishopProduct.getMarketPrice());
            map.put("销售价", unishopProduct.getSalesPrice());
            map.put("销售量", unishopProduct.getSales());
            map.put("库存", unishopProduct.getStock());
            map.put("浏览量", unishopProduct.getLook());
            map.put("已下单但是未付款量", unishopProduct.getNoBuyYet());
            map.put("是否使用规格", unishopProduct.getUseSpec());
            map.put("规格属性", unishopProduct.getSpeclist());
            map.put("规格值", unishopProduct.getSpectablelist());
            map.put("权重", unishopProduct.getWeigh());
            map.put("运费模板", unishopProduct.getDeliveryId());
            map.put("真实销量", unishopProduct.getRealSales());
            map.put("真实浏览量", unishopProduct.getRealLook());
            map.put("是否上架:0=否,1=是", unishopProduct.getSwitchStatus());
            map.put("服务", unishopProduct.getServer());
            map.put("更新时间", unishopProduct.getUpdatetime());
            map.put("创建时间", unishopProduct.getCreatetime());
            map.put("删除时间", unishopProduct.getDeletetime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}